/*
 * To change this license header, choose License Headers in Project Properties.
 * To change this template file, choose Tools | Templates
 * and open the template in the editor.
 */
package crawler.impl;

import com.google.common.base.Joiner;
import crawler.common.PostData;
import crawler.itf.PostImpl;
import crawler.util.DateUtil;
import java.util.LinkedList;
import java.util.List;
import org.apache.log4j.Logger;
import org.jsoup.nodes.Document;
import org.jsoup.nodes.Element;
import org.jsoup.select.Elements;

/**
 *
 * @author LT
 */
public class NgoisaoParser extends PostImpl {

    static final Logger logger = Logger.getLogger(NgoisaoParser.class);

    @Override
    public List<PostData> getListTitle(Document doc) {

        List<PostData> lstPost = new LinkedList<>();
        Elements eTitle1 = doc.select(".tnht .news");
        String url1 = eTitle1.select("h3 a").attr("href");
            PostData postData1 = new PostData();
            postData1.setUrl(url1);
            String thumbUrl1 = eTitle1.select("img:not(.ico-video)").attr("src");
            postData1.setThumb(StaticCrawlerImpl.crawlImage(this, doc, thumbUrl1));
            lstPost.add(postData1);


        Elements eTitle2 = doc.select(".newso ul li");
        for (Element e : eTitle2) {
            String url2 = e.select("h3 a").attr("href");

            PostData postData = new PostData();
            postData.setUrl(url2);

            String thumbUrl2 = e.select("img:not(.ico-video)").attr("src");
            postData.setThumb(StaticCrawlerImpl.crawlImage(this, doc, thumbUrl2));
            lstPost.add(postData);
        }
        Elements eTitle3 = doc.select("ul.news li");
        for (Element e : eTitle3) {
            String url3 = e.select("a.fl.ptw").attr("href");

            PostData postData = new PostData();
            postData.setUrl(url3);

            String thumbUrl3 = e.select("img:not(.ico-video)").attr("src");
            postData.setThumb(StaticCrawlerImpl.crawlImage(this, doc, thumbUrl3));
            lstPost.add(postData);
        }
        return lstPost;
    }

    @Override
    public PostData getDetail(Document doc) {
        String url = doc.location();
        String title = getTitle(doc);
        String cat = getCat(doc);
        String thumbFb = getFbThumb(doc);
        String d = getDesc(doc);
        String ctn = getContent(doc);
        PostData data = null;
        List<String> listImages = new LinkedList<>();

        Elements images = doc.select(".fck_detail img");
        for (Element image : images) {
            String org = image.attr("src");
            org = StaticCrawlerImpl.crawlImage(this, doc, org);
            listImages.add(org);
        }

        List<String> tags = getTags(doc);
        String strTags = Joiner.on(", ").join(tags);
        String jr = getJournalist(doc);
        ctn = removeJournalistFromContent(ctn, doc);
        ctn = removeIframeFromContent(ctn, doc);
        //check if content is empty
        if (ctn.length() < 100) {
            logger.error("Content empty: " + url);
            return null;
        }

        long published = getPublished(doc);

        long crtd = System.currentTimeMillis() / 1000L;
        String site = "ngoisao.net";
        String fbUrl = getFbUrl(doc);
        String fbTitle = getFbTitle(doc);
        String fbDesc = getFbDesc(doc);
        String fbSite = getFbSite(doc);
        data = new PostData(site,null, cat, "0", title, null, thumbFb, url, d, ctn, listImages, strTags, jr, published, crtd, fbUrl, fbTitle, fbDesc, fbSite);
        return data;
    }

   @Override
    public String getTitle(Document doc) {
        Elements title = doc.select("h1.title");
        String t = title!=null?title.text():null;
        return t;
    }

    @Override
    public String getCat(Document doc) {
        Elements cat = doc.select(".navigationDetail a.aNav.upperCase");
        return cat!=null?cat.text():null;
    }

    @Override
    public String getDesc(Document doc) {
        Elements intro = doc.select("h2.lead");
        return intro!=null?intro.text():null;
    }

    @Override
    public long getPublished(Document doc) {
        long published = 0;
        Elements publsh = doc.select("span.spanDateTime");
        for (Element item : publsh) {
            if(published <= 0 && item!= null && !item.text().trim().isEmpty()){
                published = DateUtil.parseDate(item.text().trim());
            }
        }
        return published;
    }

    @Override
    public String getContent(Document doc) {
        Elements content = doc.select(".fck_detail");
        String ctn = content.html();
        return ctn;
    }

    @Override
    public List<String> getTags(Document doc) {
        Elements arrTags = doc.select(".wordTag a");
        List<String> lstTags = new LinkedList<>();
        for (Element itemTag : arrTags) {
            String htmlCode = itemTag.text();
            if(htmlCode!=null && !htmlCode.trim().isEmpty()){
                lstTags.add(htmlCode.trim());
            }
        }
        return lstTags;
    }

    @Override
    public String getJournalist(Document doc) {
        String a = "";
        Elements authors = doc.select("p[style=\"text-align:right;\"]");
        if(authors.size() > 0){
            Element author = authors.last();
            a = author.text().trim();

        }
        return a;
    }
    public String removeJournalistFromContent(String ctn, Document doc){
         Elements authors = doc.select("p[style=\"text-align:right;\"]");
        String htmlAuthor = authors.outerHtml();
        ctn = ctn.replace(htmlAuthor, "");
        return ctn;
    }
    public String removeIframeFromContent(String ctn, Document doc){
         Elements iframes = doc.select("iframe");
         for (Element iframe : iframes) {
            String htmlIframe = iframe.outerHtml();
            ctn = ctn.replace(htmlIframe, "");
        }
        return ctn;
    }
}
